package fr.mma.livraison;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;

import fr.mma.livraison.controller.Factory;
import fr.mma.livraison.controller.LivraisonController;
import fr.mma.livraison.model.dto.ApplicationList;
import fr.mma.livraison.model.dto.reponse.ReponseDTO;
import fr.mma.livraison.model.dto.reponse.ReponseOK;

@SuppressWarnings("serial")
public class ApplicationServlet extends AbstractServlet<ApplicationList> {
	
	@Override
	public void doGet(final HttpServletRequest req, final HttpServletResponse resp) throws IOException {
		final LivraisonController controller = new LivraisonController();
		final Gson gson = new Gson();
		final String json = gson.toJson(Factory.buildApplication(controller.getAllApplication()));
		resp.getWriter().println(json);
	}

	@Override
	protected ReponseDTO postDTO(final ApplicationList dto, final String userName) {
		final LivraisonController controller = new LivraisonController();
		controller.saveApplication(dto.getAppList());
		return new ReponseOK();
	}

	@Override
	protected void postEmpty(final HttpServletRequest req, final HttpServletResponse resp) throws IOException {
		//RAS
	}

	@Override
	protected Class<ApplicationList> getDTOClass() {
		return ApplicationList.class;
	}
}
